import { IsNotEmpty, IsString, Length } from "class-validator";
import { BaseEntity } from "../../../common/BaseEntity";
import { TStatus } from "../../../common/CommonType";

import { Column, Entity, JoinColumn, JoinTable, ManyToMany, OneToOne, PrimaryGeneratedColumn } from "typeorm";
import SysRole from "../role/Entity";
import SysDeptEntity from "../dept/Entity";

@Entity({
  name: "sys_user",
  orderBy: { createTime: "DESC" },
})
export default class SysUserEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Length(2, 10)
  @IsString()
  @IsNotEmpty()
  @Column({
    nullable: true,
  })
  public nickname: string;

  @Length(3, 10)
  @IsString()
  @IsNotEmpty()
  @Column({
    nullable: true,
  })
  public loginId: string;

  @Length(4, 1000)
  @IsString()
  @IsNotEmpty()
  @Column({
    nullable: true,
    select: false,
  })
  public loginPwd: string;

  @IsString()
  @Column({
    nullable: true,
  })
  public userType: string;

  @IsString()
  @Column({
    nullable: true,
  })
  public email: string;

  @IsString()
  @Column({
    nullable: true,
  })
  public phone: string;

  @IsString()
  @Column({
    nullable: true,
  })
  public sex: string;

  @IsString()
  @Column({
    nullable: true,
  })
  public avatar: string;

  @IsString()
  @IsNotEmpty()
  @Column({
    nullable: true,
  })
  public status: TStatus;

  @ManyToMany(() => SysRole, (role) => role.users)
  @JoinTable()
  roles: SysRole[] | number[];

  roleNames: string;

  /**
   * 与部门进行关联
   */
  @OneToOne(() => SysDeptEntity)
  @JoinColumn()
  dept: SysDeptEntity;

  public static transform(plainObject: object): SysUserEntity {
    return super.baseTransform(SysUserEntity, plainObject);
  }
}
